package com.tbm.impl.controller;

import com.tbm.base.controller.BaseController;
import com.tbm.base.service.MenuService;
import com.tbm.base.service.RoleService;
import com.tbm.common.constant.SessionConstant;
import com.tbm.common.util.JurisdictionUtil;
import com.tbm.common.util.PageData;
import com.tbm.common.util.RightsHelper;
import com.tbm.entity.MenuEntity;
import com.tbm.entity.RoleEntity;
import com.tbm.entity.SystemUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/8/28.
 */
@Controller
public class IndexController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    /**
     * 程序入口
     * @return
     */
    @RequestMapping("/")
    public ModelAndView login(){
        ModelAndView modelAndView = this.getModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "index/main";
       if(JurisdictionUtil.isValid()){
           Object systemUser = this.getRequest().getSession().getAttribute(SessionConstant.SYSTEMUSER);
           if(systemUser instanceof SystemUser){
               String roleId = ((SystemUser) systemUser).getRoleId();
               RoleEntity roleEntity = roleService.findOne(Integer.valueOf(roleId));
               String rights = roleEntity.getRights();
               List<Integer> menuIdList = new ArrayList<Integer>();
               for(int i=0;i<10000;i++){
                   if(RightsHelper.testRights(rights,i)){
                       menuIdList.add(i);
                   }
               }
               //通过ID，获取所有的MenuEntity
               List<MenuEntity> menuList = menuService.findAll(menuIdList);
               List<PageData> nodes = new ArrayList<PageData>();
               for(MenuEntity menuEntity : menuList){
                   //获取父目录
                   if("0".equals(menuEntity.getParentId())){

                       //获取MENU_ID
                       Integer menuId = menuEntity.getMenuId();

                       PageData pd = new PageData();
                       pd.put("MENU_ID",menuEntity.getMenuId());
                       pd.put("PARENT_ID",menuEntity.getParentId());
                       pd.put("MENU_NAME",menuEntity.getMenuName());
                       pd.put("MENU_ICON",menuEntity.getMenuIcon());
                       List<PageData> subMenuEntityPageData = new ArrayList<>();

                       for(MenuEntity menu:menuList){
                           if(menuId == Integer.valueOf(menu.getParentId())){
                               PageData subPageData = new PageData();
                               subPageData.put("MENU_ID",menu.getMenuId());
                               subPageData.put("PARENT_ID",menu.getParentId());
                               subPageData.put("MENU_URL",menu.getMenuURL());
                               subPageData.put("MENU_NAME",menu.getMenuName());
                               subPageData.put("MENU_ICON",menu.getMenuIcon());
                               subMenuEntityPageData.add(subPageData);
                           }
                       }
                       pd.put("subMenu",subMenuEntityPageData);
                       nodes.add(pd);
                   }
               }
           modelAndView.addObject("menu",nodes);
           }
       }else{
           viewName = "login";
       }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    /**
     * iframe 跳转页面
     * @return
     */
    @RequestMapping(value="/iframe")
    public ModelAndView main_iframe(){
        ModelAndView mv = this.getModelAndView();
        mv.setViewName("index/welcome");
        return mv;
    }
    @RequestMapping("/logout")
    public ModelAndView logout(){
        ModelAndView modelAndView = this.getModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }


}
